package com.yzy;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import org.dozer.DozerBeanMapperBuilder;
import org.dozer.Mapper;

import java.util.*;

/**
 * @author yangzhenyu
 * @version 1.0
 * @description:
 * @date 2022/7/1 11:27
 */
class A{
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
class B{
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
class BeanUtils{
    private static final Mapper defaultMapper = DozerBeanMapperBuilder.buildDefault();

    //list复制
    public static <T> List<T> mapc(List<?> source, Class<T> destinationClass) {
        return (List)mapAndConvert(source, destinationClass);
    }
    //Set复制
    public static <T> Set<T> mapc(Set<?> source, Class<T> destinationClass) {
        return (Set)mapAndConvert(source, destinationClass);
    }
    public static <T> T map(Object source, Class<T> destinationClass) {
        return source == null ? null : defaultMapper.map(source, destinationClass);
    }
    private static <T> Collection<T> mapAndConvert(Collection<?> source, Class<T> destinationClass) {
        if (source == null) {
            return null;
        } else {
            Object result;
            if (List.class.isAssignableFrom(source.getClass())) {
                result = new ArrayList(0);
            } else if (Set.class.isAssignableFrom(source.getClass())) {
                result = new HashSet(0);
            } else {
                result = new ArrayDeque(0);
            }

            Iterator var3 = source.iterator();

            while(var3.hasNext()) {
                Object src = var3.next();
                ((Collection)result).add(map(src, destinationClass));
            }

            return (Collection)result;
        }
    }
}
public class Test5 {

    public static void main(String[] args) {
        List<A> listA = Lists.newArrayList();
        A a = new A();
        a.setName("xxx");
        listA.add(a);
        List<B> listB = Lists.newArrayList();

        listB = BeanUtils.mapc(listA, B.class);
        System.out.printf((JSON.toJSONString(listB)));
    }
}
